﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using MachineQA.Properties;

namespace MachineQA.Model
{
    partial class Location : IDataErrorInfo
    {
        public static Location CreateLocation(string name)
        {
            Location retVal = new Location();
            retVal.Name = name;
            return retVal;
        }

        #region IDataErrorInfo Implementation
        public string Error
        {
            get { return null; }
        }

        public string this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Implementation

        #region Validation

        static readonly string[] ValidatedProperties =
        {
            "Name"
        };

        string GetValidationError(string propertyName)
        {
            if(Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = ValidateName();
                    break;
                default:
                    Debug.Fail("Unexpected property being validated");
                    break;
            }
            return error;
       }

        private string ValidateName()
        {
            if (Constants.entities.Locations.Where((i) => i.Id != this.Id && i.Name == this.Name).Count() > 0)
            {
                return Resources.DuplicateLocationValidationError;
            }
            return null;
        }

        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }
        #endregion // Validation
    }
}
